home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1997 August / Walnut Creek CDROM.7z / LISTINGS / V_12_05 / ADAMS.ZIP / REFCOUNT.CPP next >
Encoding:
C/C++ Source or Header  |  1994-03-03  |  2.6 KB  |  58 lines

  1.  
  2. Listing 2.  Vector with dynamic VES and reference counting 
  3.  
  4. class Vector;
  5. class VES                // VES class definition
  6. {    unsigned lng;            // Vector Length
  7.      unsigned refs;           // Reference Count
  8.      double *ves;             // VES
  9.      friend Vector;
  10.      VES ()                   // Default Constructor
  11.      {    lng = 0;            // No VES
  12.           refs = 0;
  13.           ves = NULL; }
  14.      VES (const unsigned ln)       // New Vector VES Constructor
  15.      {    lng = ln;           // Set length
  16.           ves = new double [ln];   // Allocate new VES
  17.           refs = 0; }
  18.      VES (const VES& v)       // Copy Constructor
  19.      {    lng = v.lng;        // Set length
  20.           ves = new double [lng];  // Allocate new VES & copy
  21.           memcpy (ves, v.ves, lng * sizeof(double));
  22.           refs = 0; }
  23.      ~VES () { if (ves != NULL) delete ves; }          // VES Destructor
  24. };
  25. class Vector             // Vector class definition
  26. {    VES *x;                  // Pointer to VES wrapper
  27.     public:
  28.      Vector () {    x = NULL; }         // Default Constructor
  29.      Vector (const unsigned ln)    // New Vector Constructor
  30.      {    x = new VES (ln); }      // Create a new VES
  31.      Vector (const Vector& v) // Copy Constructor
  32.      {    x = v.x;                 // Copy VES
  33.           x->refs++; }             // Increment reference count
  34.      ~Vector ()                    // Destructor
  35.      {    x->refs--;                    // Decrement reference count
  36.           if (x->refs == 0) delete x; } // If no references, delete
  37.      double& operator()(const int coordinate) const
  38.      {    // Address elements by coordinate
  39.           return x->ves[coordinate]; }
  40.      unsigned length() const { return x->lng; } // Report Vector length
  41.      Vector& operator= (const Vector&);      // Assignment operator
  42.      Vector operator+ (const Vector&) const; // Addition operator
  43.      friend Vector unit (const Vector&);          // Unitize Vector
  44. };
  45. Vector& Vector::operator= (const Vector& v)  // Assignment operator
  46. {    if (ves != v.ves) {      // Protect against self-reference
  47.           if (x != NULL) {    // If current VES has no other references,
  48.                if (--(x->refs) == 0) delete x; } // delete VES
  49.           x = v.x; }          // Copy reference to VES
  50.      return *this; }
  51. Vector Vector::operator+ (const Vector& v)   // Vector addition
  52. {    if (v.x->lng != x->lng) exit(1);   // Ensure conformal addition
  53.      Vector u (x->lng);            // Create same size Vector
  54.      for (int i = 0; i < x->lng; i++)   // Carry out addition
  55.           u.x->ves[i] = x->ves[i] + v.x->ves[i];
  56.      return u; }
  57.  
  58.